<template>
  <div id="customers">
    <div class="card">
      <div class="card-header bg-info text-white">企业-订单备注</div>
      <div class="card-body">
        <form class="row">
          <div class="col-2">
            <my-select-customer
              :customerId.sync="customerId0"
              :minId="-1"
            ></my-select-customer>
          </div>
          <div class="col-1">
            <select class="form-select" v-model.number="noteType0">
              <option value="0">所有</option>
              <option value="100">成本中心</option>
              <option value="200">项目名称</option>
            </select>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-1"
              @click="search()"
            >
              查找
            </button>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button
              type="button"
              class="btn btn-secondary btn-sm"
              title="清空查找条件"
              @click="reset()"
            >
              重置
            </button>

            <a
              href="javascript:void(0)"
              v-on:click="newNote()"
              class="btn btn-success ms-auto float-end"
              >新建</a
            >
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-sm small">
      <thead>
        <tr>
          <th>公司</th>
          <th>类型</th>
          <th>名称</th>
          <th>创建时间</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(info, index) in dataList">
          <td>{{ info.customer.vipName }}</td>
          <td>
            <template v-if="info.noteType === 100">成本中心</template>
            <template v-if="info.noteType === 200">项目名称</template>
          </td>
          <td>{{ info.name }}</td>
          <td>{{ info.createTime }}</td>
          <td>
            <button
              class="btn btn-danger btn-sm"
              @click="deleteCustomerNote(info.id)"
            >
              删除
            </button>
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-customer-note ref="customerNoteModal">
      <span slot="title">{{ modalTitle }}</span>
    </my-modal-customer-note>
  </div>
</template>

<script>
import {
  searchCustomerNotes,
  createCustomerNote,
  deleteCustomerNote,
} from "@/api/customer.js";
import $ from "jquery";
import MyPagination from "@/components/my-pagination.vue";
import MySelectCustomer from "@/components/my-select2-customer.vue";
import MyModalCustomerNote from "@/components/modal/my-modal-customer-note.vue";

export default {
  components: {
    MyPagination,
    MySelectCustomer,
    MyModalCustomerNote,
  },
  data() {
    return {
      id: 0,
      customerId: 0,
      noteType: 100,
      name: "",

      dataList: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },
      customerId0: -1,
      noteType0: 0,

      modalTitle: "",
    };
  },
  mounted: function () {
    this.search();
    if (window.matchMedia("(max-width: 576px)").matches) {
      console.log(this.$router);
      this.$router.replace("/h5/customer/cost-centers");
    } else {
      this.search();
    }
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        customerId: this.customerId0,
        noteType: this.noteType0,
      };

      searchCustomerNotes(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    newNote: function () {
      this.modalTitle = "增加订单注释内容";
      this.$refs.customerNoteModal
        .modal()
        .then(v => {
          this.saveNote(v)
        })
        .catch((ex) => {});
    },
    saveNote: function (v) {
      const params = {
        customerId: v.customerId,
        noteType: v.noteType,
        name: v.name,
      };

      createCustomerNote(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg);
        } else {
          this.search();
        }
      });
    },
    deleteCustomerNote: function (id) {
      deleteCustomerNote(id, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg);
        } else {
          this.search();
        }
      });
    },
    reset: function () {
      this.name = "";
      this.sc.pageNo = 1;
      this.customerid0 = -1;
      this.noteType0 = 0;
      this.search();
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
